home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / vfwdk.zip / VFWSDK.ZIP / SAMPLES / MSRLEC / PLAYRLE.C < prev    next >
C/C++ Source or Header  |  1993-01-31  |  3KB  |  140 lines

  1. /*****************************************************************************
  2.  *
  3.  *  PlayRLE.C - RLE display code
  4.  *
  5.  *   Copyright (c) 1992-1993 Microsoft Corporation.  All Rights Reserved.
  6.  *
  7.  *    You have a royalty-free right to use, modify, reproduce and 
  8.  *    distribute the Sample Files (and/or any modified version) in 
  9.  *    any way you find useful, provided that you agree that 
  10.  *    Microsoft has no warranty obligations or liability for any 
  11.  *    Sample Application Files which are modified. 
  12.  *
  13.  ****************************************************************************/
  14.  
  15. #include <windows.h>
  16. #include "rle.h"
  17.  
  18. #define RLE_ESCAPE  0
  19. #define RLE_EOL     0
  20. #define RLE_EOF     1
  21. #define RLE_JMP     2
  22. #define RLE_RUN     3
  23.  
  24. extern WORD PASCAL __WinFlags;
  25. #define WinFlags (WORD)(&__WinFlags)
  26.  
  27. typedef BYTE huge * HPRLE;
  28. typedef BYTE far  * LPRLE;
  29.  
  30. void NEAR PASCAL DecodeRle(LPBITMAPINFOHEADER lpbi, HPRLE  pb, HPRLE prle);
  31.  
  32. //
  33. // these are in RLEA.ASM
  34. //
  35. void NEAR PASCAL DecodeRle286(LPBITMAPINFOHEADER lpbi, LPBYTE pb, LPBYTE prle);
  36. void NEAR PASCAL DecodeRle386(LPBITMAPINFOHEADER lpbi, LPBYTE pb, LPBYTE prle);
  37.  
  38. #define DibPtr(hdib) ((LPBYTE)GlobalLock(hdib) + \
  39.             (int)((LPBITMAPINFOHEADER)GlobalLock(hdib))->biSize + \
  40.             (int)((LPBITMAPINFOHEADER)GlobalLock(hdib))->biClrUsed * sizeof(RGBQUAD) )
  41.  
  42. void PlayRle(LPBITMAPINFOHEADER lpbi, LPVOID pDib, LPVOID pRLE)
  43. {
  44.     DWORD dw;
  45.  
  46.     dw = (DWORD)(WORD)lpbi->biHeight * (DWORD)(WORD)lpbi->biWidth;
  47.  
  48.     if (dw < 65536l)
  49.         DecodeRle286(lpbi, pDib, pRLE);
  50.     else if (WinFlags & WF_CPU286)
  51.         DecodeRle(lpbi, pDib, pRLE);
  52.     else
  53.         DecodeRle386(lpbi, pDib, pRLE);
  54. }
  55.  
  56. //  PlayRleDib
  57. //
  58. //  Play back a RLE buffer into a DIB
  59. //
  60. //      hdib        - dest DIB
  61. //      x,y         - position in dest DIB where to place RLE
  62. //      hrle        - src RLE
  63. //
  64. //  returns
  65. //
  66. //      none
  67. //
  68. void PlayRleDib(HANDLE hdib, HANDLE hrle)
  69. {
  70.     PlayRle((LPBITMAPINFOHEADER) GlobalLock(hdib), DibPtr(hdib), DibPtr(hrle));
  71. }
  72.  
  73. //
  74. //  DecodeRle   - 'C' version
  75. //
  76. //  Play back a RLE buffer into a DIB buffer
  77. //
  78. //  returns
  79. //      none
  80. //
  81. void NEAR PASCAL DecodeRle(LPBITMAPINFOHEADER lpbi, HPRLE pb, HPRLE prle)
  82. {
  83.     BYTE    cnt;
  84.     BYTE    b;
  85.     WORD    x;
  86.     WORD    dx,dy;
  87.     WORD    wWidthBytes;
  88.  
  89.     wWidthBytes = (WORD)lpbi->biWidth+3 & ~3;
  90.  
  91.     x = 0;
  92.  
  93.     for(;;)
  94.     {
  95.         cnt = *prle++;
  96.         b   = *prle++;
  97.  
  98.         if (cnt == RLE_ESCAPE)
  99.         {
  100.             switch (b)
  101.             {
  102.                 case RLE_EOF:
  103.                     return;
  104.  
  105.                 case RLE_EOL:
  106.                     pb += wWidthBytes - x;
  107.                     x = 0;
  108.                     break;
  109.  
  110.                 case RLE_JMP:
  111.                     dx = (WORD)*prle++;
  112.                     dy = (WORD)*prle++;
  113.  
  114.                     pb += (DWORD)wWidthBytes * dy + dx;
  115.                     x  += dx;
  116.  
  117.                     break;
  118.  
  119.                 default:
  120.                     cnt = b;
  121.                     x  += cnt;
  122.                     while (cnt-- > 0)
  123.                         *pb++ = *prle++;
  124.  
  125.                     if (b & 1)
  126.                         prle++;
  127.  
  128.                     break;
  129.             }
  130.         }
  131.         else
  132.         {
  133.             x += cnt;
  134.  
  135.             while (cnt-- > 0)
  136.                 *pb++ = b;
  137.         }
  138.     }
  139. }
  140.